using UnityEngine;
using System.Collections;

public class GameScript : MonoBehaviour {
		
	private Transform m_transform;
	private Spawner m_spawner;

	// Use this for initialization
	void Start () {
		m_transform = transform;
		m_spawner = m_transform.GetComponent<Spawner>();
		m_spawner.SpawnPrefab("ammo", "1");
		m_spawner.SpawnPrefab("health", "3");
	}
	
	void TestGameStatus(){
		GameObject[] humans = GameObject.FindGameObjectsWithTag("human");
		GameObject[] aliens = GameObject.FindGameObjectsWithTag("alien");
		int humans_dead = 0;
		int aliens_dead = 0;
		//Debug.Log ( humans.Length +", "+aliens.Length  );
		foreach( GameObject human in humans){
			PlayerScript player_game;
			player_game = human.GetComponent<PlayerScript>();
			if(player_game.myHealth < 0){
				humans_dead++;
			}
			//Debug.Log(human.name+", "+player_game.myHealth+ ", H dead: "+ humans_dead);
		}
		foreach( GameObject alien in aliens){
			PlayerScript player_game;
			player_game = alien.GetComponent<PlayerScript>();
			if(player_game.myHealth < 0){
				aliens_dead++;
			}
			//Debug.Log(alien.name+", "+player_game.myHealth+ ", A dead: "+ aliens_dead);
		}
		if(humans_dead >= 2){// 2 v 2
			Messenger<string>.Invoke("reset","aliens_win");
		}
		else if(aliens_dead >= 2){// 2 v 2
			Messenger<string>.Invoke("reset","humans_win");
		}
	}
	
	// Update is called once per frame
	void Update () {
		TestGameStatus();
	}
}
